/**
 * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "GPUImageFalseColorFilter.h"
#include "OpenGlUtils.h"

const std::string GPUImageFalseColorFilter::FALSECOLOR_FRAGMENT_SHADER = R"(
    #extension GL_OES_EGL_image_external : require
    precision lowp float;

    varying highp vec2 textureCoordinate;

    uniform samplerExternalOES inputImageTexture;
    uniform float intensity;
    uniform vec3 firstColor;
    uniform vec3 secondColor;

    const mediump vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);

    void main()
    {
        lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
        float luminance = dot(textureColor.rgb, luminanceWeighting);

        gl_FragColor = vec4(mix(firstColor.rgb, secondColor.rgb, luminance), textureColor.a);
    }
)";

GPUImageFalseColorFilter::GPUImageFalseColorFilter()
    : firstColor(new float[]{0.0f, 0.0f, 0.5f}),
      secondColor(new float[]{1.0f, 0.0f, 0.0f}) {}

GPUImageFalseColorFilter::GPUImageFalseColorFilter(
    float firstRed, float firstGreen, float firstBlue, float secondRed,
    float secondGreen, float secondBlue)
    : GPUImageFilter(NO_FILTER_VERTEX_SHADER, FALSECOLOR_FRAGMENT_SHADER),
      firstColor(new float[3]{firstRed, firstGreen, firstBlue}),
      secondColor(new float[3]{secondRed, secondGreen, secondBlue}) {}

GPUImageFalseColorFilter::GPUImageFalseColorFilter(float *firstColorArr,
                                                   float *secondColorArr)
    : GPUImageFilter(NO_FILTER_VERTEX_SHADER, FALSECOLOR_FRAGMENT_SHADER)
{
    firstColor = firstColorArr;
    secondColor = secondColorArr;
}

GPUImageFalseColorFilter::~GPUImageFalseColorFilter()
{
    delete[] firstColor;
    delete[] secondColor;
    firstColor = nullptr;
    secondColor = nullptr;
}

void GPUImageFalseColorFilter::onInit()
{
    GPUImageFilter::onInit();
    firstColorLocation = glGetUniformLocation(getProgram(), "firstColor");
    secondColorLocation = glGetUniformLocation(getProgram(), "secondColor");
}

void GPUImageFalseColorFilter::onInitialized()
{
    GPUImageFilter::onInitialized();
    setFirstColor(firstColor);
    setSecondColor(secondColor);
}

void GPUImageFalseColorFilter::setFirstColor(float *firstColorArr)
{
    firstColor = firstColorArr;
    setFloatVec3(firstColorLocation, firstColor);
}

void GPUImageFalseColorFilter::setSecondColor(float *secondColorArr)
{
    secondColor = secondColorArr;
    setFloatVec3(secondColorLocation, secondColor);
}